package com.baosight.sczx.zm.cf.service;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cf.MessageUtil;
import com.baosight.sczx.common.zm.cInterface.cl.RiskRecord;
import com.baosight.sczx.common.zm.cInterface.cl.TernateDeregulation;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cf.domain.ZMCFPenalty;

public class ServiceZMCF21 extends ServiceEPBase {

	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:14:44 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		
		EiInfo outInfo = this.query(inInfo);
		
		String fdSendnoteDepartmentId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();//登陆人所在部门
		String fdSendnoteDepartment = Department.getInstance().getDepartmentNameById(fdSendnoteDepartmentId);
		outInfo.set("fdSendnoteDepartment", fdSendnoteDepartment);//登陆人所在部门名称
		outInfo.set("fdSendnoteDepartmentId", fdSendnoteDepartmentId);//登陆人所在部门ID 
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.penaltyType", false));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.penaltyType", "cmes.cl.penaltyTypes", true));
		outInfo.addBlock(Department.getInstance().getConstructionUnit(false));
		
		return outInfo;
	}

	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:14:50 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdSendnoteDepartmentId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();//登陆人所在部门
		String fdSendnotePersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//开单人ID
		
		StaticTools.setQueryParameters(inInfo, "fdSendnoteDepartmentId", fdSendnoteDepartmentId, true);//添加查询条件 开单单位 为 登陆人所在单位
		StaticTools.setQueryParameters(inInfo, "fdSendnotePersonId", fdSendnotePersonId, true);//添加查询条件 开单人 为 登陆人
		
		EiInfo outInfo = super.query(inInfo, "ZMCFPenalty.query", new ZMCFPenalty());
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:01 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdKeyId = TablePk.getInstance().getNextValue("T_CF_NOTICE");
		inInfo.setCell("result", 0, "fdKeyId", fdKeyId);//主健
		inInfo.setCell("result", 0, "fdStatus", "草拟");//状态
		
		String fdPeopleId = inInfo.getCellStr("result", 0, "fdPeopleId");//责任人ID
		String fdDepartmentId = inInfo.getCellStr("result", 0, "fdDepartmentId");//责任单位ID
		String fdSendnoteDepartmentId = inInfo.getCellStr("result", 0, "fdSendnoteDepartmentId");//开单部门
		String fdSendnotePersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//开单人ID
		String fdSendnotePerson = UserSession.getInSessionProperty("UserCname") == null ? "" : UserSession.getInSessionProperty("UserCname").toString();//开单人
		
		if(null == fdSendnoteDepartmentId || "".equals(fdSendnoteDepartmentId)) {
			fdSendnoteDepartmentId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();
			inInfo.setCell("result", 0, "fdSendnoteDepartmentId", fdSendnoteDepartmentId);
		}
		
		inInfo.setCell("result", 0, "fdSendnoteDepartment", Department.getInstance().getDepartmentNameById(fdSendnoteDepartmentId));
		inInfo.setCell("result", 0, "fdSendnotePersonId", fdSendnotePersonId);
		inInfo.setCell("result", 0, "fdSendnotePerson", fdSendnotePerson);
		
		if(fdPeopleId != null && !fdPeopleId.equals("")) {
			//String fdPeopleName = User.getInstance().getNameById(fdPeopleId);
			inInfo.setCell("result", 0, "fdPeopleName", fdPeopleId);//责任人
		}
		
		if(fdDepartmentId != null && !fdDepartmentId.equals("")) {
			String fdDepartmentName = Department.getInstance().getDepartmentNameById(fdDepartmentId);
			inInfo.setCell("result", 0, "fdDepartmentName", fdDepartmentName);//责任单位
		}
		
		EiInfo outinfo = super.insert(inInfo, "ZMCFPenalty.insert", "result");
		return outinfo;
	}

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:10 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		String fdPeopleId = inInfo.getCellStr("result", 0, "fdPeopleId");//责任人ID
		String fdDepartmentId = inInfo.getCellStr("result", 0, "fdDepartmentId");//责任单位ID
		
		String fdSendnotePersonId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//开单人ID
		String fdSendnotePerson = UserSession.getInSessionProperty("UserCname") == null ? "" : UserSession.getInSessionProperty("UserCname").toString();//开单人
		
		if(fdPeopleId != null && !fdPeopleId.equals("")) {
			//String fdPeopleName = User.getInstance().getNameById(fdPeopleId);
			inInfo.setCell("result", 0, "fdPeopleName", fdPeopleId);//责任人
		}
		
		if(fdDepartmentId != null && !fdDepartmentId.equals("")) {
			String fdDepartmentName = Department.getInstance().getDepartmentNameById(fdDepartmentId);
			inInfo.setCell("result", 0, "fdDepartmentName", fdDepartmentName);//责任单位
		}
		
		inInfo.setCell("result", 0, "fdSendnotePersonId", fdSendnotePersonId);//开单人ID
		inInfo.setCell("result", 0, "fdSendnotePerson", fdSendnotePerson);//开单人
		
		EiInfo outInfo = super.update(inInfo, "ZMCFPenalty.update");//主表更新

		return outInfo;
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCF15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:28 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCFPenalty.delete", "result");
		
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 生效
	 * <p>Function: release</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-31上午07:24:30 严洪浩
	 * </p>
	 *
	 */
	public EiInfo release(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCFPenalty.release");
		
		//发消息
		if(_temp.getStatus() != -1)
		{
			String fdPenalty = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdPenalty");//罚款金额
			String fdDepartmentId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdDepartmentId");//责任人
			
			MessageUtil messageUtil = new MessageUtil();
			messageUtil.setDao(dao);
			int result = messageUtil.insertIntoMessageCache("被开" + fdPenalty + "元的罚款单", "ZMCF20", fdDepartmentId, "", 1);
			
			if(result != 1)
			{
				System.out.println("消息发送失败："+result);
			}
		}
		//end
		
		EiInfo outInfo = this.query(inInfo);
       
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 已交罚款
	 * <p>Function: penalty</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-8上午06:50:09 严洪浩
	 * </p>
	 *
	 */
	public EiInfo penalty(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCFPenalty.penalty");
		EiInfo outInfo = this.query(inInfo);
       
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 作废
	 * <p>Function: invalid</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-8上午06:50:09 严洪浩
	 * </p>
	 *
	 */
	public EiInfo invalid(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCFPenalty.invalid");
		
		TernateDeregulation.UpdatePenalty(inInfo.getCellStr("result", 0, "fdKeyId"));//更新三违记录
		RiskRecord.UpdatePenalty(inInfo.getCellStr("result", 0, "fdKeyId"));//更新隐患记录
		
		EiInfo outInfo = this.query(inInfo);
       
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
}
